package com.ps.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ps.constant.StatusConstant;
import com.ps.entity.Medicine;
import com.ps.entity.Orders;
import com.ps.entity.Setmeal;
import com.ps.entity.User;
import com.ps.mapper.MedicineMapper;
import com.ps.mapper.OrderMapper;
import com.ps.mapper.SetmealMapper;
import com.ps.mapper.UserMapper;
import com.ps.service.OrderService;
import com.ps.service.WorkspaceService;
import com.ps.vo.BusinessDataVO;
import com.ps.vo.MedicineOverViewVO;
import com.ps.vo.OrderOverViewVO;
import com.ps.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.print.attribute.IntegerSyntax;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.ps.entity.Orders.*;

@Service
@Slf4j
public class WorkspaceServiceImpl extends ServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MedicineMapper medicineMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        Integer totalOrderCount = orderService.lambdaQuery()
                .ge(Orders::getOrderTime,begin)
                .le(Orders::getOrderTime,end).count().intValue();
        log.info("总订单数：{}", totalOrderCount);

        List<Object> list =orderMapper.selectObjs(new LambdaQueryWrapper<Orders>()
                .eq(Orders::getStatus, COMPLETED)
                .ge(Orders::getOrderTime,begin)
                .le(Orders::getOrderTime, end)
                .select(Orders::getAmount));
        //将list集合里的每一个元素拿出来相加，最终结果转换成Double类型
        Double turnover = list.stream()
                .mapToDouble(o -> ((Number) o).doubleValue()).sum();
        log.info("营业额：{}", turnover);
        turnover = turnover==null? 0.0 : turnover;
        //有效订单数
        Integer validOrderCount = orderMapper.selectCount(new LambdaQueryWrapper<Orders>()
                .ge(Orders::getOrderTime,begin)
                .le(Orders::getOrderTime,end)
                .eq(Orders::getStatus, COMPLETED)).intValue();
        log.info("有效订单数：{}", validOrderCount);
        Double unitPrice = 0.0;
        Double orderCompletionRate = 0.0;
        if(totalOrderCount != 0 && validOrderCount != 0){
            //订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
            //平均客单价
            unitPrice = turnover / validOrderCount;
        }
        //新增用户数
        Integer newUsers = userMapper.selectCount(new LambdaQueryWrapper<User>()
                .ge(User::getCreateTime, begin)
                .le(User::getCreateTime, end)).intValue();
        log.info("新增用户数：{}", newUsers);

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }

    public OrderOverViewVO getOrderOverView() {
        //待接单
        Integer waitingOrders = orderMapper.selectCount(new LambdaQueryWrapper<Orders>()
                        .gt(Orders::getOrderTime,LocalDateTime.now().with(LocalTime.MIN))
                        .eq(Orders::getStatus,TO_BE_CONFIRMED))
                .intValue();
        //待派送
        Integer deliveredOrders = orderMapper.selectCount(new LambdaQueryWrapper<Orders>()
                .gt(Orders::getOrderTime,LocalDateTime.now().with(LocalTime.MIN))
                .eq(Orders::getStatus,Orders. CONFIRMED))
                .intValue();
        //已完成
        Integer completedOrders = orderMapper.selectCount(new LambdaQueryWrapper<Orders>()
                .gt(Orders::getOrderTime,LocalDateTime.now().with(LocalTime.MIN))
                .eq(Orders::getStatus, COMPLETED))
                .intValue();
        //已取消
        Integer cancelledOrders = orderMapper.selectCount(new LambdaQueryWrapper<Orders>()
                .gt(Orders::getOrderTime,LocalDateTime.now().with(LocalTime.MIN))
                .eq(Orders::getStatus,CANCELLED))
                .intValue();
        //全部订单
        Integer allOrders = orderMapper.selectCount(new LambdaQueryWrapper<Orders>()
                .gt(Orders::getOrderTime,LocalDateTime.now().with(LocalTime.MIN)))
                .intValue();
        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }


    public MedicineOverViewVO getMedicineOverView() {
        // 查询启用状态的药品数量
        Integer sold = medicineMapper.selectCount(new LambdaQueryWrapper<Medicine>()
                .eq(Medicine::getStatus, StatusConstant.ENABLE)).intValue();
        // 查询禁用状态的药品数量
        Integer discontinued = medicineMapper.selectCount(new LambdaQueryWrapper<Medicine>()
                .eq(Medicine::getStatus, StatusConstant.DISABLE)).intValue();
        return MedicineOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }


    public SetmealOverViewVO getSetmealOverView() {
        Integer sold = setmealMapper.selectCount(new LambdaQueryWrapper<Setmeal>()
                        .eq(Setmeal::getStatus,StatusConstant.ENABLE)).intValue();
        Integer discontinued =setmealMapper.selectCount(new LambdaQueryWrapper<Setmeal>()
                        .eq(Setmeal::getStatus,StatusConstant.DISABLE)).intValue();
        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
